package com.example.vaccine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vaccine.VO.PageVO;
import com.example.vaccine.entity.VaccineUsage;
import com.example.vaccine.entity.Vaccines;
import com.example.vaccine.mapper.VaccineUsageMapper;
import com.example.vaccine.service.VaccineUsageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vaccine.service.VaccinesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Coisn
 * @since 2025-06-12
 */
@Service
public class VaccineUsageServiceImpl extends ServiceImpl<VaccineUsageMapper, VaccineUsage> implements VaccineUsageService {

    @Autowired
    private VaccineUsageMapper vaccineUsageMapper;

    @Autowired
    private VaccinesService vaccinesService;

    @Override
    public List<Map<String, Object>> getUsageRecords(Integer doctorId) {
        return baseMapper.selectUsageWithVaccineName(doctorId);
    }

    @Override
    public PageVO queryUsageList(Integer doctorId, String vaccineName, String startDate, String endDate, Integer page, Integer size) {
        QueryWrapper<VaccineUsage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("doctor_id", doctorId);

        if (StringUtils.hasText(vaccineName)) {
            queryWrapper.like("name", vaccineName);
        }
        if (StringUtils.hasText(startDate)) {
            queryWrapper.ge("usage_time", startDate);
        }
        if (StringUtils.hasText(endDate)) {
            queryWrapper.le("usage_time", endDate);
        }

        Page<VaccineUsage> pageParam = new Page<>(page, size);
        Page<VaccineUsage> resultPage = vaccineUsageMapper.selectPage(pageParam, queryWrapper);

        PageVO pageVO = new PageVO();
        pageVO.setData(resultPage.getRecords());
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }

    @Transactional
    public boolean receiveVaccine(VaccineUsage usage) {
        // 1. 查询疫苗库存
        Vaccines vaccine = vaccinesService.getByNumbering(usage.getVaccineNumbering());
        if (vaccine == null) {
            return false;
        }

        // 2. 检查库存是否充足
        if (vaccine.getAmount() < usage.getQuantityUsed()) {
            return false;
        }

        // 3. 扣减库存
        vaccine.setAmount(vaccine.getAmount() - usage.getQuantityUsed());
        boolean updateResult = vaccinesService.updateById(vaccine);
        if (!updateResult) {
            return false;
        }

        // 4. 设定使用时间并保存领取记录
        usage.setUsageTime(new Date());
        int insertCount = vaccineUsageMapper.insert(usage);
        return insertCount > 0;
    }
}
